<?php

namespace app\logic;

use app\model\Option as OptionModel;

class OptionLogic
{
    /**
     * @var OptionModel
     */
    private OptionModel $optionModel;

    /**
     * @param OptionModel $optionModel
     */
    public function __construct(OptionModel $optionModel)
    {
        $this->optionModel = $optionModel;
    }

    public function getSelectOption($num, $level = 0)
    {
        $pid = $this->getPid($num);
        return $this->optionModel
            ->where('pid', $pid)
            ->where('valid', 1)
            ->order('sort,id')
            ->column('name');
    }

    /**
     * @param $num
     * @return int|mixed|string
     */
    private function getPid($num)
    {
        if (!is_numeric($num)) {
            $id = $this->optionModel->where('num', $num)->value('id', 0);
            if ($id == 0) {
                $id = -829;
            }
        } else {
            $id = $num;
        }

        return $id;
    }
}